package cn.item.buckle.confg;

import cn.lsh.util.FileSystemUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

public enum ConfigEnum {

	/**/
	HIVE_METASTORE_ADDR("hive.metastore.addr", "thrift://node00:9083", "hive metastore 服务地址"),
	SPARK_STANDALONE_ADDR("spark.standalone.addr", "spark://node00:7077", "Spark standalone资源调度集群地址"),
	HIVE_WAREHOUSE_DIR("hive.warehouse.dir", "/user/hive_remote/warehouse", "hive数据文件在hdfs中的目录"),
	HIVE_DATABASE("hive.database", "traffic", "项目的hive数据库名"),
	MONITOR_FLOW_ACTION("monitor.flow.action", "monitor_flow_action", "车辆抓拍记录表"),
	MONITOR_ACTION_DATE("monitor.action.date", "0", "车辆抓拍记录表date字段"),
	MONITOR_ACTION_TIME_TYPE("monitor.action.time_type", "yyyy-MM-dd HH:mm:ss", "车辆抓拍记录表actioneTime字段日期格式"),
	MONITOR_CAMERA_INFO("monitor.camera.info", "monitor_camera_info", "监控id和抓拍探头id绑定关系表"),
	LOAD_DATA_SOURCE("load.data.source", "local", "本地还是hdfs加载数据，本地配置为local，hdfs配置为hdfs"),
	LOAD_DATA_OVERWRITE("load.data.overwrite", "true", "load是追加还是覆盖数据，默认为追加"),
	MONITOR_FLOW_ACTION_FILE("monitor.flow.action.file", "/学习/大数据/项目/卡扣监控/data/monitor_flow_action.txt", "车辆抓拍记录表数据存放目录"),
	MONITOR_CAMERA_INFO_FILE("monitor.camera.info.file", "/学习/大数据/项目/卡扣监控/data/monitor_camera_info.txt", "监控id和抓拍探头id绑定关系表数据存放目录"),
	LOCAL_DATA_DIR("local.data.dir", "G:\\学习\\大数据\\项目\\卡扣监控\\data\\", "本地数据存放目录"),
	MONITOR_CAR_COUNT_TOP_N("monitor.car.count.top.n", "5", "取车辆通过数的topN的卡扣"),
	// APPLICATION_TASK_ID("application.task.id", "1111111111", "应用从程序taskID"),
	APPLICATION_TASK_ID("application.task.id", "2222222222", "应用从程序taskID"),
	MONITOR_CAR_SPEED_TOP_N("monitor.car.speed.top.n", "5", "取车辆通过速度的topN的卡扣"),
	MONITOR_CAR_SPEED_HIGH("monitor.car.speed.high", "120", "车速定义高速的下限"),
	MONITOR_CAR_SPEED_MEDIUM("monitor.car.speed.medium", "90", "车速定义中速的下限"),
	MONITOR_CAR_SPEED_NORMAL("monitor.car.speed.normal", "60", "车速定义正常的下限"),
	MONITOR_CAR_TRACK_MONITOR("monitor.car.track.monitor", "0000", "求所有车辆的轨迹的起始经过卡扣"),
	MONITOR_CAR_TRACK_PARTITION("monitor.car.track.partition", "100", "保存车辆轨迹结果的分区数"),
	TAKE_SAMPLE_CAR_ONE_DAY("task.sample.car.one.day", "100", "抽取一天的车辆总数"),
	MONITOR_CAR_CONVERSION_RATE("monitor.car.conversion.rate", "0001,0002,0003,0004,0005", "抽取一天的车辆总数"),
	;

	private static final Logger log = LoggerFactory.getLogger(ConfigEnum.class);

	private static Properties properties = new Properties();

	static {
		/*boolean isSuccess = loadHdfsProperties();
		if (!isSuccess) {
			//如果hdfs上没有配置文件，就加载本地的，本地也没有，就是用默认的
			loadLocalProperties();
		}
		for (Map.Entry<Object, Object> entry : properties.entrySet()) {
			log.info("---" + entry.getKey() + "=" + entry.getValue());
		}*/
	}

	private static boolean loadHdfsProperties() {
		InputStream inputStream = null;
		log.info("load hdfs application properties");
		try {
			String confPath = "/item/resources/conf/item_application.properties";
			Path path = new Path(confPath);
			FileSystem fileSystem = FileSystemUtil.getFileSystem();
			if (fileSystem.exists(path)) {
				inputStream = fileSystem.open(path);
				properties.load(inputStream);
				return true;
			} else {
				log.warn("not found hdfs application properties -- /item/resources/conf/item_application.properties");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}

	private static boolean loadLocalProperties() {
		log.info("load local application properties");
		InputStream inputStream = null;
		try {
			inputStream = CommonConfig.class.getClassLoader().getResourceAsStream("item_application.properties");
			if (inputStream != null) {
				properties.load(inputStream);
				return true;
			} else {
				log.warn("not found local application properties -- resources:/item_application.properties");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}

	ConfigEnum(String name, String value, String desc) {
		this.name = name;
		this.value = value;
		this.desc = desc;
	}

	private String name;

	private String value;

	private String desc;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		String userConf = properties.getProperty(this.name);
		if (StringUtils.isBlank(userConf)) {
			userConf = this.value;
		}
		return userConf;
	}

	public int getIntValue() {
		return Integer.parseInt(this.getValue());
	}

	public long getLongValue() {
		return Long.parseLong(this.getValue());
	}

	private void setValue(String value) {
		this.value = value;
	}
}
